//
//  Player_Vol_Pos_Bar.m
//  ICE_Player
//
//  Created by  on 11-10-19.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "Player_Vol_Pos_Bar.h"
#import "PlayerViewController.h"

@implementation Player_Vol_Pos_Bar

@synthesize p_btn;
@synthesize is_dragging;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc {
	[self destroy_SubViews];
    [super dealloc];
}

- (void)create_SubViews:(UIViewController*)controller
{
    [super create_SubViews:controller];
    
    p_controller = controller;
    is_dragging = NO;
}

- (void)scale_to_fit_iPad
{
    CGRect rect = self.bounds;
    CGPoint pnt = {0};
    /*rect.origin.x = 100.f;
    rect.origin.y = 768.f - 100.f;
    rect.size.width = 1024.f - 100.f - 100.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    self.bounds = rect;
    self.center = pnt;*/
}

- (void) setHidden:(BOOL)enabled
{
    if (enabled)
    {
        CATransition *animation = [CATransition animation];
        animation.delegate = self;
        animation.duration = 0.15f;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
        animation.type = kCATransitionFade;//@"rippleEffect";//110
        //animation.subtype = kCATransitionFromRight;//kCATransitionFromTop;
        //animation.endProgress = 1.0;
        //animation.removedOnCompletion = NO;
        
        //[self.layer addAnimation:animation forKey:@"animation"];
        [self.layer addAnimation:animation forKey:@"animation"];
        
    }
    else
    {
        CATransition *animation = [CATransition animation];
        animation.delegate = self;
        animation.duration = 0.15f;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
        animation.type = kCATransitionFade;//@"rippleEffect";//110
        //animation.subtype = kCATransitionFromRight;//kCATransitionFromTop;
        //animation.endProgress = 1.0;
        //animation.removedOnCompletion = NO;
        
        //[self.layer addAnimation:animation forKey:@"animation"];
        [self.layer addAnimation:animation forKey:@"animation"];
    }
	super.hidden = enabled;
}

- (void)destroy_SubViews
{
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("B");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    is_dragging = YES;
    [p_btn set_Btn_Pos:pt];
    p_btn.highlighted = YES; 
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("M");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    [p_btn set_Btn_Pos:pt];
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("C");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    [p_btn set_Btn_Pos:pt];
    if (p_controller)
    {
        pt.y = pt.y > p_btn.high_y ? p_btn.high_y : (pt.y < p_btn.low_y ? p_btn.low_y : pt.y);
        [((PlayerViewController*)p_controller) On_Set_Vol:((p_btn.high_y - pt.y) * 100.f / (p_btn.high_y - p_btn.low_y))];
    }
    is_dragging = NO;
    p_btn.highlighted = NO; 
    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("E");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    [p_btn set_Btn_Pos:pt];
    if (p_controller)
    {
         pt.y = pt.y > p_btn.high_y ? p_btn.high_y : (pt.y < p_btn.low_y ? p_btn.low_y : pt.y);
        [((PlayerViewController*)p_controller) On_Set_Vol:((p_btn.high_y - pt.y) * 100.f / (p_btn.high_y - p_btn.low_y))];
    }
    is_dragging = NO;
    p_btn.highlighted = NO; 
    [super touchesEnded:touches withEvent:event];
}

- (void)Set_Vol_Btn_On_Volume:(float)percents
{
    if (is_dragging || p_btn.is_dragging)
    {
        return;
    }
    if (p_btn)
    {
        CGPoint pt = p_btn.center;
        pt.y = p_btn.low_y + (CGFloat)((float)(p_btn.high_y - p_btn.low_y) * ((100.f - percents) / 100.f));
        [p_btn set_Btn_Pos:pt];
    }
}

- (float)Get_Cur_Percents
{
    CGPoint pt = {0};
    float ret = 0.f;
    if (p_btn)
    {
        pt = p_btn.center;
        ret = (p_btn.high_y - pt.y) * 100.f / (p_btn.high_y - p_btn.low_y);
    }
    return ret;
}

@end
